package com.site.web.sys.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;

import com.site.domain.BannerInfo;
import com.site.domain.UserInfo;
import com.site.service.BannerInfoService;

/**
 * Purpose: 案例
 * @author: liangjun
 * @since: JDK 1.6
 * @date: 2015年9月22日
 */
@Controller
@RequestMapping("/banner/")
public class BannerInfoController {
	private static Logger logger = LoggerFactory.getLogger(BannerInfoController.class);
	private static String SYS_LOGIN = "redirect:/sys/login";
	@Autowired
	private BannerInfoService bannerInfoService;
	
	/**
	 * Purpose: banner查询
	 * @author: liangjun
	 * @param request
	 * @param response
	 * @return: ModelAndView
	 */
	@RequestMapping(value="findBanner", method=RequestMethod.GET)
	public ModelAndView findBanner(HttpServletRequest request,HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		UserInfo userInfo = null;
		userInfo = (UserInfo) WebUtils.getSessionAttribute(request, "userInfo");
		if(null == userInfo){
			WebUtils.setSessionAttribute(request, "userInfo", null);
			return new ModelAndView(SYS_LOGIN);
		}
		List<BannerInfo> bannerInfoList = null;
		try {
			bannerInfoList = bannerInfoService.findBannerInfo();
			if(null != bannerInfoList){
				mav.addObject("bannerInfoList", bannerInfoList);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("banner查询异常:"+e.getMessage()+"");
		}
		return mav;
	}
	
	@RequestMapping(value="addBanner", method=RequestMethod.GET)
	public ModelAndView addBanner(HttpServletRequest request,HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		UserInfo userInfo = null;
		userInfo = (UserInfo) WebUtils.getSessionAttribute(request, "userInfo");
		if(null == userInfo){
			WebUtils.setSessionAttribute(request, "userInfo", null);
			return new ModelAndView(SYS_LOGIN);
		}
		return mav;
	}
	
	/**
	 * Purpose: 
	 * @author: liangjun
	 * @param request
	 * @param response
	 * @param BannerInfo
	 * @return: ModelAndView
	 */
	@RequestMapping(value="addBanner", method=RequestMethod.POST)
	public ModelAndView addBanner(HttpServletRequest request,HttpServletResponse response,@ModelAttribute BannerInfo bannerInfo) {
		ModelAndView mav = new ModelAndView();
		UserInfo userInfo = null;
		userInfo = (UserInfo) WebUtils.getSessionAttribute(request, "userInfo");
		if(null == userInfo){
			WebUtils.setSessionAttribute(request, "userInfo", null);
			return new ModelAndView(SYS_LOGIN);
		}
		int resultCode = 0;
		String resultMsg = "";
		try {
			bannerInfoService.addBannerInfo(bannerInfo);
			resultCode = 1;
			resultMsg = "添加成功";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("添加异常:"+e.getMessage()+"");
			resultMsg = "添加异常";
		}
		mav.addObject("resultCode", resultCode);
		mav.addObject("resultMsg", resultMsg);
		return mav;
	}
	
	@RequestMapping(value="updBanner", method=RequestMethod.GET)
	public ModelAndView updBanner(HttpServletRequest request,HttpServletResponse response,String id) {
		ModelAndView mav = new ModelAndView();
		UserInfo userInfo = null;
		userInfo = (UserInfo) WebUtils.getSessionAttribute(request, "userInfo");
		if(null == userInfo){
			WebUtils.setSessionAttribute(request, "userInfo", null);
			return new ModelAndView(SYS_LOGIN);
		}
		int resultCode = 0;
		String resultMsg = "";
		BannerInfo bannerInfo = null;
		try {
			bannerInfo = bannerInfoService.getBannerInfoById(Integer.parseInt(id));
			if(null != bannerInfo){
				resultCode = 1;
				mav.addObject("bannerInfo", bannerInfo);
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("参数ID类型转换异常:"+e.getMessage()+"");
			resultMsg = "获取失败，无编号ID";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("查询异常:"+e.getMessage()+"");
			resultMsg = "获取失败，查询异常";
		}
		mav.addObject("resultCode", resultCode);
		mav.addObject("resultMsg", resultMsg);
		return mav;
	}
	
	@RequestMapping(value="updBanner", method=RequestMethod.POST)
	public ModelAndView updBanner(HttpServletRequest request,HttpServletResponse response,@ModelAttribute BannerInfo bannerInfo) {
		ModelAndView mav = new ModelAndView();
		UserInfo userInfo = null;
		userInfo = (UserInfo) WebUtils.getSessionAttribute(request, "userInfo");
		if(null == userInfo){
			WebUtils.setSessionAttribute(request, "userInfo", null);
			return new ModelAndView(SYS_LOGIN);
		}
		int resultCode = 0;
		String resultMsg = "";
		try {
			bannerInfoService.updBannerInfo(bannerInfo);
			resultCode = 1;
			resultMsg = "修改成功";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("修改异常:"+e.getMessage()+"");
			resultMsg = "修改异常";
		}
		mav.addObject("resultCode", resultCode);
		mav.addObject("resultMsg", resultMsg);
		return mav;
	}
	

	@RequestMapping(value="delBanneById", method=RequestMethod.POST)
	public ModelAndView delBanneById(HttpServletRequest request,HttpServletResponse response,String id) {
		ModelAndView mav = new ModelAndView();
		UserInfo userInfo = null;
		userInfo = (UserInfo) WebUtils.getSessionAttribute(request, "userInfo");
		if(null == userInfo){
			WebUtils.setSessionAttribute(request, "userInfo", null);
			return new ModelAndView(SYS_LOGIN);
		}
		int resultCode = 0;
		String resultMsg = "";
		try {
			bannerInfoService.delBannerInfoById(Integer.parseInt(id));
			resultCode = 1;
			resultMsg = "更新成功";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("更新异常:"+e.getMessage()+"");
			resultMsg = "更新异常";
		}
		mav.addObject("resultCode", resultCode);
		mav.addObject("resultMsg", resultMsg);
		return mav;
	}
	
}
